﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public static class FileUtility
{
    /// <summary>
    /// 创建文件夹
    /// </summary>
    /// <param name="path">文件夹路径</param>
    static public void CreateFolder(string path)
    {
        if (Directory.Exists(path))
            return;
        Directory.CreateDirectory(path);
    }

    static public void DeleteFolder(string path)
    {
        if (Directory.Exists(path))
            Directory.Delete(path, true);
    }

    static public List<string> GetFolderNamesInFolder(string folderPath)
    {
        List<string> folders = new List<string>();
        string[] folderPaths = Directory.GetDirectories(folderPath, "*", SearchOption.TopDirectoryOnly);
        for (int i = 0; i < folderPaths.Length; i++)
        {
            string objPath = folderPaths[i];
            if (objPath.Contains("."))
                continue;
            objPath = objPath.Replace(@"\", "/");
            string folderName = objPath.Substring(objPath.LastIndexOf("/") + 1);
            folders.Add(folderName);
        }
        return folders;
    }

    /// <summary>
    /// 获取文件夹下的文件名
    /// </summary>
    /// <param name="folderPath">文件夹路径</param>
    /// <param name="appointExtensionName">指定后缀</param>
    /// <param name="exceptExtensionName">排除后缀</param>
    /// <returns></returns>
    static public List<string> GetFileNamesInFolder(string folderPath, string appointExtensionName, bool removeExtensionName = false, string exceptExtensionName = "", bool includChild = false)
    {
        List<string> files = new List<string>();
        try
        {
            string cutString;
            if (folderPath.EndsWith("/"))
            {
                cutString = folderPath;
                folderPath = folderPath.Remove(folderPath.Length - 1, 1);
            }
            else
            {
                cutString = folderPath + "/";
            }
            string[] filePaths;
            if (includChild)
                filePaths = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
            else
                filePaths = Directory.GetFiles(folderPath, "*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < filePaths.Length; i++)
            {
                string objPath = filePaths[i];
                if (objPath.Contains(".meta"))
                    continue;
                if (!string.IsNullOrEmpty(appointExtensionName))
                {
                    if (!objPath.EndsWith(appointExtensionName.ToUpper()) && !objPath.EndsWith(appointExtensionName.ToLower()))
                    {
                        continue;
                    }
                }
                if (!string.IsNullOrEmpty(exceptExtensionName))
                {
                    if (objPath.EndsWith(exceptExtensionName.ToUpper()) || objPath.EndsWith(exceptExtensionName.ToLower()))
                    {
                        continue;
                    }
                }
                objPath = objPath.Replace(@"\", "/");
                string[] split = System.Text.RegularExpressions.Regex.Split(objPath, cutString);
                if (split.Length < 2)
                {
                    Debug.Log(objPath + "-" + folderPath);
                }
                else
                {
                    string name = split[1];
                    if (removeExtensionName)
                    {
                        if (string.IsNullOrEmpty(appointExtensionName))
                        {
                            string[] dotSplit = name.Split('.');
                            if (dotSplit.Length > 1)
                                name = dotSplit[0];
                        }
                        else
                        {
                            name = name.Replace(appointExtensionName.ToUpper(), "");
                            name = name.Replace(appointExtensionName.ToLower(), "");
                        }
                    }
                    files.Add(name);
                }
            }
        }
        catch (System.IO.DirectoryNotFoundException)
        {
            Debug.LogError(folderPath + "不存在");
        }
        return files;
    }

    /// <summary>
    /// 获取文件夹下的文件路径
    /// </summary>
    /// <param name="folderPath">文件夹路径</param>
    /// <param name="appointExtensionName">指定后缀</param>
    /// <param name="exceptExtensionName">排除后缀</param>
    /// <returns></returns>
    static public List<string> GetFilePathsInFolder(string folderPath, string appointExtensionName = "", string exceptExtensionName = "", bool includChild = false)
    {
        List<string> files = new List<string>();
        try
        {
            string[] filePaths;
            if (includChild)
                filePaths = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
            else
                filePaths = Directory.GetFiles(folderPath, "*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < filePaths.Length; i++)
            {
                string objPath = filePaths[i];
                if (objPath.Contains(".meta"))
                    continue;
                if (!string.IsNullOrEmpty(appointExtensionName))
                {
                    if (!objPath.EndsWith(appointExtensionName.ToUpper()) && !objPath.EndsWith(appointExtensionName.ToLower()))
                    {
                        continue;
                    }
                }
                if (!string.IsNullOrEmpty(exceptExtensionName))
                {
                    if (objPath.EndsWith(exceptExtensionName.ToUpper()) || objPath.EndsWith(exceptExtensionName.ToLower()))
                    {
                        continue;
                    }
                }
                objPath = objPath.Replace(@"\", "/");
                files.Add(objPath);
            }
        }
        catch (System.IO.DirectoryNotFoundException)
        {
            Debug.LogError(folderPath + "不存在");
        }
        return files;
    }

    /// <summary>
    /// 获取文件MD5码
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    static public string GetMD5Hash(string filePath)
    {
        if (!File.Exists(filePath))
        {
            Debug.LogError("不存在" + filePath);
            return "";
        }
        try
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("Get file MD5 fail,error:" + ex.Message);
        }
    }

    /// <summary>
    /// 获取文件大小
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    static public long GetFileSize(string filePath)
    {
        try
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            long length = file.Length;
            file.Close();
            return length;
        }
        catch (Exception ex)
        {
            throw new Exception("Get file size fail,error:" + ex.Message);
        }
    }

    /// <summary>
    /// 获取文件夹大小
    /// </summary>
    /// <param name="folderPath"></param>
    /// <returns></returns>
    static public long GetFolderSize(string folderPath)
    {
        try
        {
            long size = 0;
            if (!Directory.Exists(folderPath))
                return 0;
            DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
            foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                size += fileInfo.Length;
            return size;
        }
        catch (Exception ex)
        {
            throw new Exception("Get folder size fail,error:" + ex.Message);
        }
    }
}
[Serializable]
public class PackageInfo
{
    public int fileNumber;
    public string[] fileName;
    public int[] fileSize;
}